import {Component, OnInit} from '@angular/core';
import {GoodsStatusService} from './goods-status.service';
import {LocalStorageService} from '../../../../lib/service/local-storage.service';
import {AliyunOssOutputUrl} from '../../../../../config/api';

@Component({
  selector: 'app-goods-status',
  templateUrl: 'goods-status.component.html',
  styleUrls: ['goods-status.component.scss'],
  providers: [GoodsStatusService, LocalStorageService]
})
export class GoodsStatusComponent implements OnInit {


  loadingShow = true;
  aliyunOssOutputUrl;
  nowApplet: any;
  keyword: string;
  groupId = 0;  // 搜索栏中选中的分组ID
  goodsList = [];
  groupList = [];
  order = 'none';  // 订单排序
  status = '上架';  // 当前列表商品的状态
  isCheckedAll = false;  // 当前页是否选中所有
  checkedGoods = [];  // 选中的商品ID集合

  type;
  message;
  isShow;

  totalItems: number;
  itemsPerPage = 12;
  currentPage = 1;

  constructor(private goodsStatusService: GoodsStatusService,
              private localStorageService: LocalStorageService) {
    this.aliyunOssOutputUrl = AliyunOssOutputUrl;
  }

  ngOnInit() {
    this.nowApplet = this.localStorageService.getNowApplet();
    this.getGroupList();
    this.getGoodsList(1, 0, this.status);
  }

  // 获取商品列表
  getGoodsList(page: number, applet_goods_category_id?: number, status?: string, order?: string, keyword?: string) {
    this.loadingShow = true;
    this.currentPage = null;
    this.goodsStatusService.getGoodsList(this.nowApplet.applet_uuid, page, 12, applet_goods_category_id, status, order, keyword).subscribe(
      res => {
        if (res.code === 2000) {
          setTimeout(
            () => {
              this.loadingShow = false;
            }
            ,500);
          const goodsList = res.data.data;
          const ids = [];
          this.currentPage = res.data.current_page;
          this.totalItems = res.data.total;
          goodsList.every((val, idx, array) => {
            ids.push(val.id);
            this.checkedGoods.every((val1, idx1, array1) => {
              if (val1 === val.id) {
                val.selected = true;
                return false;
              } else {
                val.selected = false;
                return true;
              }
            });
            return true;
          });

          this.goodsStatusService.getbindListAll(JSON.stringify(ids)).subscribe(
            res1 => {
              this.goodsList = goodsList;
              goodsList.every((val, idx, array) => {
                val.goods_category = [];
                res1.data.every((val1, idx1, array1) => {
                  if (val.id === val1.goods_id) {
                    val.goods_category.push({name: val1.name, id: val1.applet_goods_category_id});
                  }
                  return true;
                });
                return true;
              });
            }
          );
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 获取商品分组列表
  getGroupList() {
    this.goodsStatusService.getGroupList(this.nowApplet.applet_uuid).subscribe(
      res => {
        this.groupList = res.data.data;
      }
    );
  }

  // 选中所有
  checkedAll() {
    this.goodsList.every((val, idx, array) => {
      // 全不选
      if (this.isCheckedAll === val.selected) {
        this.checkedGoods.every((val1, idx1, array1) => {
          if (val.id === val1) {
            this.checkedGoods.splice(idx1, 1);
            return false;
          }
        });
      }
      // 全选
      if (!this.isCheckedAll && !val.selected) {
        this.checkedGoods.push(val.id);
      }
      val.selected = !this.isCheckedAll;
      return true;
    });
    new Set(this.checkedGoods);
  }

  // checkbox 框操作存储id
  selectedGoods(item) {
    if (!item.selected) {
      this.checkedGoods.push(item.id);
    } else {
      this.checkedGoods.every((val, idx, array) => {
        if (val === item.id) {
          this.checkedGoods.splice(idx, 1);
          return false;
        }
        return true;
      });
    }
  }

  search() {
    this.getGoodsList(1, this.groupId, this.status, this.order, this.keyword);
  }

  reset() {
    this.keyword = '';
    this.groupId = 0;
    this.order = 'none';
    this.getGoodsList(1, this.groupId, this.status, this.order, this.keyword);
  }

  // 切换当前显示列表中商品的状态
  changeStatusList(status: string) {
    this.status = status;
    this.checkedGoods = [];
    this.isCheckedAll = false;
    this.getGoodsList(1, this.groupId, this.status, this.order, this.keyword);
  }

  // 改变商品上下架状态
  changeGoodsStatus(status: string, id?: number) {
    let ids = [];
    if (!!id) {
      ids.push(id);
    } else {
      ids = this.checkedGoods;
    }
    if (!ids.length) {
      this.type = 'info';
      this.message = '请至少选择一件商品';
      this.isShow = true;
      return false;
    }
    const data = {applet_uuid: this.nowApplet.applet_uuid, ids: JSON.stringify(ids), status: status};
    this.goodsStatusService.changeGoodsStatus(data).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '修改成功';
          this.isShow = true;
          this.getGoodsList(1, this.groupId, this.status, this.order, this.keyword);
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  pageChanged(event: any): void {
    window.scrollTo(0,0);
    this.isCheckedAll = false;
    this.getGoodsList(event.page, this.groupId, this.status, this.order, this.keyword);
  }
}
